<?php
namespace app\bis\controller;
use app\bis\controller\Base;
class Location extends Base{
    
    public function index(){
        $bisId=$this->getLoginUser()->bis_id;
        $locations=model("BisLocation")->getNormalLocaltionsByBisId($bisId);
        return $this->fetch("",[
            "locations"=>$locations,
        ]);
    }
    
    public function add(){
       if(request()->isPost()){
           $data=input("post.");
           $validate=validate("BisLocation");
           //validate验证
          if(!$validate->check($data)){
            return  $this->error($validate->getError());
          }
           $bisId=$this->getLoginUser()->bis_id;
           $res=model("Bis")->get(["id"=>$bisId]);
           //银行卡
           $bank_info=$res->bank_info;
         
          //地图上的xpoint  ypoint
          $lnglat= \Map::getLngLat($data['address']);
         if(empty($lnglat) || $lnglat['status'] !=0 ||  $lnglat['result']['precise'] !=1){
            $this->error("无法获取数据 或者 数据不准确");
         }
         
         //category_path
          $data['cat']="";
         if(!empty($data['se_category_id'])){
             $data['cat']= implode("|",$data['se_category_id']);
          } 
          //总店信息相关入库其中$bisId是登陆用户从session中的值
         $locationData=[
             'bis_id'=>$bisId,
             'name' => $data['name'],
             'logo' => $data['logo'],
             'tel' => $data['tel'],
             'bank_info' =>$bank_info,
             'contact' => $data['contact'],
             'category_id' => $data['category_id'],
             'category_path'=>$data["category_id"].",".$data['cat'],//cat
             'city_id'=>$data['city_id'],
             'city_path'=>empty($data['se_city_id'])?$data['city_id']:$data['city_id'].','.$data['se_city_id'],
             'api_address'=>$data['address'],
             'address'=>$data['address'],
             'open_time'=>$data['open_time'],
             'content'=>empty($data['content'])? '' : $data['content'],
             'is_main'=>0,//代表分店
             'status'=>0,//审核状态
             'xpoint' =>empty($lnglat['result']['location']['lng']) ? '' : $lnglat['result']['location']['lng'],
             'ypoint' =>empty($lnglat['result']['location'][ 'lat']) ? '' : $lnglat['result']['location']['lat'],
         ]; 
          //入库并判断
         $locationId=model("BisLocation")->add($locationData);
         if($locationId){   
             $this->success("添加分店成功");
         }else{
             $this->error("添加分店失败");
         }
           
           
       }else{
        //获取城市的一级栏目
        $citys=model("City")->getNormalFirstCitys();
        //获取服务类别栏目
        $categorys=model("Category")->getNormalFirstCategorys();
        return  $this->fetch('',[
            'citys'=>$citys,
            'categorys'=>$categorys,
        ]);
       }
    }
    public function status(){
         if(!request()->isGet()){
           $this->error("非法请求");
       }
       $data=input("get.");
       $validate= validate('BisLocation');
       if(!$validate->scene("status")->check($data)){
           $this->error($validate->getError());
       }
      $res=model("BisLocation")->save(["status"=>$data['status']],["id"=>$data['id']]);  
      if($res){
          $this->success("修改成功");
      }else{
           $this->error("修改失败");
      }
    }
    
}
